/**
 * Created by Administrator on 15-11-4.
 */
var global_price=0;
var storage=window.localStorage;

function choosedate(){
    var storage=window.localStorage;
    var supplierId= $.getUrlParam("supplierId");
    storage.setItem("supplierId",supplierId);
    var num=$(".Amount").val();

    storage.setItem("num",num);
    var mobile=$(".tele").val();
    storage.setItem("mobile",mobile);
    location.href="jiudian_xuanzeriqi.html";
}
window.onload=function(){
    var supplierId = $.getUrlParam("supplierId");
    var url = getUrl();
    var userId = getUserId();
    $.jsonp({
        url: 'http://'+url+'/api/supplier/supplier_detail-'+supplierId,
        data: {},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            global_price=data.supplier.price;
            $("#totalprice").text(global_price);
            var complied_tpl = juicer($("#info_tpl").text());
            var html = complied_tpl.render(data);
            $("#top").append(html);
        },
        error: function (xOptions, textStatus) {
        }
    });
    $(document).on("click",".ui-btn-lg",function(){
        var number = $(".Amount").val();
        var telephone = $(".tele").val();
        var houseTime=$("#startdate").text();
        if(telephone=="")
        {
            showToast("手机号不能为空~");
            return;
        }
        if(telephone.length!=11)
        {
            showToast("手机号格式不对~");
            return;
        }
        if(houseTime == ""){
            showToast("请选择入住时间~");
            return;
        }
        $.jsonp({
            url: 'http://'+url+'/api/order/submit_order',
            data: {supplierId:supplierId,number:number,buyTelphone:telephone,houseTime:houseTime,userId:userId},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
                window.location = "jiudian_zhifudingdan.html?orderId="+data.orderId;
               // $("#commit").attr("href","jiudian_zhifudingdan.html?orderId="+data.orderId);
            },
            error: function (xOptions, textStatus) {
            }
        });
    });
    function GetDateDiff(startDate,endDate)
    {
        var startTime = new Date(Date.parse(startDate.replace(/-/g,   "/"))).getTime();
        var endTime = new Date(Date.parse(endDate.replace(/-/g,   "/"))).getTime();
        var dates = Math.abs((startTime - endTime))/(1000*60*60*24);
        return  dates;
    }
    (function(){

        if(storage.getItem("mobile"))
            $(".tele").val(storage.getItem("mobile"));


        if(storage.getItem("num")){
            $(".Amount").val(storage.getItem("num"));

        }
        if(storage.getItem("startdate")){
            $("#startdate").text(storage.getItem("startdate"))
        }
        if(storage.getItem("enddate")){
            $("#enddate").text(storage.getItem("enddate"))
        }
        if(storage.getItem("startdate")&&storage.getItem("enddate")){
            var days=GetDateDiff(storage.getItem("startdate"),storage.getItem("enddate"));
            console.log("相差天数",days)
            if($("#number").length>0)
                $("#number").text(days);

        }
    })();

    $(document).on("click",".Increase",function(){
        calc();
        $("#shuliang").text( $(".Amount").val());
    })
    $(document).on("click",".Decrease",function(){
        calc();
        $("#shuliang").text( $(".Amount").val());
    })
    $(document).on("click",".DisDe",function(){
        calc();
        $("#shuliang").text( $(".Amount").val());
    })


    function calc(){
        //console.log($(".Amount").val())
        var amount=parseInt($(".Amount").val())*global_price*parseInt($("#number").text());
        $("#totalprice").text(amount)

    }

}
